<?php

1->pom
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-core</artifactId>
  <version>1.2.3</version>
</dependency>
<dependency>
  <groupId>ch.qos.logback</groupId>
  <artifactId>logback-classic</artifactId>
  <version>1.2.3</version>
</dependency>

<dependency>
  <groupId>net.logstash.logback</groupId>
  <artifactId>logstash-logback-encoder</artifactId>
  <version>5.1</version>
</dependency>


2->配置文件 (logback-spring.xml)

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <springProperty name="env" source="spring.profiles.active" />
    // 配置日志的存放位置 - [一般读取 (外部) 配置文件]
    <property name="logPath" value="logstash" />
    // 配置日志平台日志名称 - [一般读取 (外部) 配置文件]
    <property name="applicationName" value="spring.application.name" />

    <appender name="LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <destination>${logPath}</destination>
        <encoder class="net.logstash.logback.encoder.LogstashEncoder" >
            <customFields>{"application_name":"${applicationName}","host":"${hostName}"}</customFields>
        </encoder>

        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
    </appender>


    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
    <springProfile name="dev">
        // name="xx.xxx.xxxx"
        <logger name="cn.com.connext" level="DEBUG"/>
        <root level="INFO">
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <springProfile name="test">
        <root level="INFO">
            <appender-ref ref="LOGSTASH"/>
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>
    <springProfile name="prod">
        <root level="INFO">
            <appender-ref ref="LOGSTASH"/>
            <appender-ref ref="CONSOLE"/>
        </root>
    </springProfile>

</configuration>
